package bairui.international.console.web.controller.personnel.employee;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.lianjing.sh.spring.web.controller.base.action.Action;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_Department;
import bairui.international.model.domain.jpa.TB_Position;
import bairui.international.service.Service_Admin;
import bairui.international.service.Service_Department;
import bairui.international.service.Service_Position;


/**
 * @generated
 *
 * C_Admin_Personnel_Employee_ExcelExport
 *
 */
@Controller
public class C_Personnel_Employee_ExcelExport
 extends SC_Personnel_Employee_ExcelExport 
{
	
	@Autowired @Lazy private Service_Admin service_Admin;
	@Autowired @Lazy private Service_Department service_Department;
	@Autowired @Lazy private Service_Position service_Position;
	
	/**
	 * 导出Excel文件
	 */
	@SuppressWarnings("serial")
	@Action(ExcelExport.class)
	@GetMapping(value=ExcelExport.RequestMappingValue)
	public void excelExport(
		HttpServletRequest request,
		HttpServletResponse response
	) throws Exception {
		List<TB_Admin> list = service_Admin.distinctList(
				it->C_Personnel_Employee_Page.Page.Filter.query(it, request),
				it->ExcelOutput_ExcelExport_Output.sort(it, request, new ExcelOutput_ExcelExport_Output.OrderExtends() {
						@Override public Sort gainPositionName(Direction direction) {
							return sort(direction, it.positions.any().name);
						}
						@Override public Sort gainDepartmentName(Direction direction) {
							return sort(direction, it.departments.any().name);
						}
					})
			);
		//
		ExcelOutput_ExcelExport_Output.output(
				list
				.stream()
				.map(it->{
					return new ExcelOutput_ExcelExport_Output.Model(it){
							@Override protected void assign_before() {
								
							}
							@Override public String assignPositionName() {
								return service_Position
									.list(it2->it2.admins.any().eq(it))
									.stream()
									.map(TB_Position::getName)
									.collect(Collectors.toList())
									.toString();
							}
							@Override public String assignDepartmentName() {
								return service_Department
										.list(it2->it2.admins.any().eq(it))
										.stream()
										.map(TB_Department::getName)
										.collect(Collectors.toList())
										.toString();
							}
						};
				})
				.collect(Collectors.toList()),
				request, 
				response
			);
	}
	

}


